package com.example.administrator.hualangrecyclerview.shop;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.example.administrator.hualangrecyclerview.BitmapUtil;
import com.example.administrator.hualangrecyclerview.R;
import com.example.administrator.hualangrecyclerview.RevealDrawable;

import java.util.List;

/**
 * Created by yarolegovich on 07.03.2017.
 */

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {

    private List<Item> data;

    public ShopAdapter(List<Item> data) {
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.item_shop_card, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.itemView.post(new Runnable() {
            @Override
            public void run() {
                Glide.with(holder.itemView.getContext())
                        .load(data.get(position).getImage())
                        .asBitmap()
                        .skipMemoryCache(true)
                        .into(new SimpleTarget<Bitmap>(holder.image.getWidth() / 2, holder.image.getHeight() / 2) {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                Drawable unselected = new BitmapDrawable(BitmapUtil.toGrayscale(resource));
                                Drawable selected = new BitmapDrawable(resource);
                                Drawable drawable = new RevealDrawable(unselected, selected, RevealDrawable.HORIZONTAL);
                                holder.image.setImageDrawable(drawable);
//                                holder.image.setImageBitmap(resource);
                            }
                        });
            }
        });
//        Glide.with(holder.itemView.getContext())
//                .load(data.get(position).getImage())
//                .asBitmap()
//                .skipMemoryCache(true)
//                .into(new SimpleTarget<Bitmap>(200, 200) {
//                    @Override
//                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                        Drawable selected = new BitmapDrawable(resource);
//                        Drawable unselected = new BitmapDrawable(BitmapUtil.toGrayscale(resource));
//                        Drawable drawable = new RevealDrawable(unselected, selected, RevealDrawable.HORIZONTAL);
//                        holder.image.setBackgroundDrawable(drawable);
//                        holder.image.setImageBitmap(resource);
//                    }
//                });
//        Glide.with(holder.itemView.getContext())
//                .load(data.get(position).getImage())
//                .into(holder.image);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
        }
    }
}
